Ext.define('Seq.svg.Resizer', {
	        extend : 'Seq.svg.Base',

	        boxCls : {
		        fill : 'none',
		        stroke : '#55aaff',
		        'stroke-width' : 1,
		        'stroke-linecap' : 'round',
		        'stroke-linejoin' : 'round',
		        'stroke-dasharray' : "5,5"
	        },

	        anchor : null, // 拖动的锚点
	        startXy : null, // 起点
	        starBox : null, // 起始矩形

	        box : null, // model的选中框阴影, 在缩放中绘制
	        selected : [], // 选中的model阴影, 在缩放中绘制

	        start : function(anchor, e) {
		        var me = this;
		        me.cancel();
		        me.anchor = anchor;
		        me.startXy = me.getXy(e);
		        me.startBox = me.canvas.selectBox.box.getBBox();
	        },

	        move : function(e) {
		        var me = this;
		        if (me.startXy) {
			        if (me.box) {
				        me.resizeShadow(e);
			        } else {
				        me.initShadow(e);
			        }
		        }
	        },

	        end : function(e) {
		        var me = this;
		        if (me.box) {
			        me.resizeSelected(e);
		        }
		        me.cancel();
	        },

	        cancel : function() {
		        var me = this;
		        if (me.box) {
			        me.box.remove();
			        me.box = null;
		        }
		        // 移除选中的MODEL阴影
		        Ext.each(me.selected, function(model) {
			                model.remove();
		                });
		        me.selected = [];
		        me.startXy = null;
		        me.startBox = null;
	        },

	        initShadow : function(e) {
		        var me = this, xy = me.getXy(e);
		        if (0 > Math.abs(xy.x - me.startXy.x) || 0 > Math.abs(xy.y - me.startXy.y)) {
			        return;
		        }

		        var paper = me.canvas.svg.paper, selectBox = me.canvas.selectBox;
		        var bbox = selectBox.box.getBBox();
		        me.box = paper.rect(bbox.x, bbox.y, bbox.w, bbox.h).attr(me.boxCls);

		        me.selected = [];
		        Ext.each(selectBox.selected, function(model) {
			                var clone = model.clone().attr({
				                        opacity : 0.6,
				                        "stroke-opacity" : 0.6,
				                        "fill-opacity" : 0.6
			                        });
			                clone.originalMatrix = clone.transform().localMatrix;
			                me.selected.push(clone);
		                });
	        },

	        getScale : function(x0, y0, x1, y1, x, y) {
		        var scale = {
			        sx : 1,
			        sy : 1,
			        x : x,
			        y : y
		        };
		        if (x0 === x1) {
			        scale.sy = Math.abs(y - y0) / Math.abs(y1 - y0);
		        } else if (y0 === y1) {
			        scale.sx = Math.abs(x - x0) / Math.abs(x1 - x0);
		        } else {
			        y = (y1 - y0) / (x1 - x0) * (x - x0) + y0;
			        // var len0 = Snap.path.getTotalLength(Ext.String.format("M{0} {1}L{2} {3}", x0, y0, x1, y1));
			        // var len1 = Snap.path.getTotalLength(Ext.String.format("M{0} {1}L{2} {3}", x0, y0, x, y));
			        scale.sx = Math.abs(y - y0) / Math.abs(y1 - y0);
			        scale.sy = scale.sx;
			        scale.y = y;
		        }
		        return scale;
	        },

	        caculateScale : function(xy) {
		        var me = this, box = me.startBox, scale;
		        switch (me.anchor.attr('anchor')) {
		        case 'nw-resize' :
			        scale = me.getScale(box.cx, box.cy, box.x, box.y, Math.min(xy.x, box.cx - 5));
			        scale.x0 = scale.x;
			        scale.y0 = scale.y;
			        scale.cx = box.x2;
			        scale.cy = box.y2;
			        break;
		        case 'n-resize' :
			        scale = me.getScale(box.cx, box.cy, box.cx, box.y, box.cx, Math.min(xy.y, box.cy - 5));
			        scale.x0 = box.x;
			        scale.y0 = scale.y;
			        scale.cx = box.cx;
			        scale.cy = box.y2;
			        break;
		        case 'ne-resize' :
			        scale = me.getScale(box.cx, box.cy, box.x2, box.y, Math.max(xy.x, box.cx + 5));
			        scale.x0 = box.x;
			        scale.y0 = scale.y;
			        scale.cx = box.x;
			        scale.cy = box.y2;
			        break;
		        case 'e-resize' :
			        scale = me.getScale(box.cx, box.cy, box.x2, box.cy, Math.max(xy.x, box.cx + 5), box.cy);
			        scale.x0 = box.x;
			        scale.y0 = box.y;
			        scale.cx = box.x;
			        scale.cy = box.cy;
			        break;
		        case 'se-resize' :
			        scale = me.getScale(box.cx, box.cy, box.x2, box.y2, Math.max(xy.x, box.cx + 5));
			        scale.x0 = box.x;
			        scale.y0 = box.y;
			        scale.cx = box.x;
			        scale.cy = box.y;
			        break;
		        case 's-resize' :
			        scale = me.getScale(box.cx, box.cy, box.cx, box.y2, box.cx, Math.max(xy.y, box.cy + 5));
			        scale.x0 = box.x;
			        scale.y0 = box.y;
			        scale.cx = box.cx;
			        scale.cy = box.y;
			        break;
		        case 'sw-resize' :
			        scale = me.getScale(box.cx, box.cy, box.x, box.y2, Math.min(xy.x, box.cx - 5));
			        scale.x0 = scale.x;
			        scale.y0 = box.y;
			        scale.cx = box.x2;
			        scale.cy = box.y;
			        break;
		        case 'w-resize' :
			        scale = me.getScale(box.cx, box.cy, box.x, box.cy, Math.min(xy.x, box.cx - 5), box.cy);
			        scale.x0 = scale.x;
			        scale.y0 = box.y;
			        scale.cx = box.x2;
			        scale.cy = box.cy;
			        break;
		        }
		        return scale;
	        },

	        resizeShadow : function(e) {
		        var me = this, xy = me.getXy(e), scale = me.caculateScale(xy), matrix = new Snap.Matrix();
		        matrix.scale(scale.sx, scale.sy, scale.cx, scale.cy);

		        me.resizeBox(scale, me.box);
		        for (var i = 0, len = me.selected.length; i < len; i++) {
			        var item = me.selected[i];
			        m = item.originalMatrix.clone();
			        m.add(matrix);
			        item.transform(m);
		        }
	        },

	        resizeBox : function(scale, box) {
		        var me = this;
		        box.attr('x', scale.x0);
		        box.attr('y', scale.y0);
		        box.attr('width', Math.abs(scale.x - scale.cx));
		        box.attr('height', Math.abs(scale.y - scale.cy));
	        },

	        resizeItems : function(items, matrix) {
		        for (var i = 0, len = items.length; i < len; i++) {
			        var item = items[i], m = item.transform().globalMatrix.add(matrix);
			        item.transform(m);
		        }
	        },

	        resizeSelected : function(e) {
		        var me = this, selectBox = me.canvas.selectBox;
		        var xy = me.getXy(e), scale = me.caculateScale(xy), matrix = new Snap.Matrix();
		        matrix.scale(scale.sx, scale.sy, scale.cx, scale.cy);

		        me.resizeItems(selectBox.selected, matrix);
		        me.resizeItems(selectBox.subBoxs, matrix);

		        var box = me.canvas.selectBox.box;
		        me.resizeBox(scale, box);
		        selectBox.drawAnchors(me.canvas.svg.paper, box.getBBox());
	        }

        });